package com.shopping.service.shoppingCat;


import com.shopping.constant.ResponseCode;
import com.shopping.dao.ShoppingCartDao;
import com.shopping.pojo.ShoppingCart;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShoppingCatServiceImpl implements ShoppingCatService{

    @Autowired
    ShoppingCartDao shoppingCartDao;

    @Override
    public Map<String, Object> insert(ShoppingCart shoppingCart) {
        Map<String, Object> map = new HashMap<>();
        try{
            shoppingCartDao.insert(shoppingCart);
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
        }catch (Exception e){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
        }
        return map;
    }

    @Override
    public Map<String, Object> delete(Integer id) {
        Map<String, Object> map = new HashMap<>();
        try {
            shoppingCartDao.delete(id);
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
        }catch (Exception e){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
        }
        return map;
    }

    @Override
    public Map<String, Object> modifyShoppingCat(ShoppingCart shoppingCart) {
        Map<String, Object> map = new HashMap<>();
        try {
            shoppingCartDao.update(shoppingCart);
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
        }catch (Exception e){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
        }
        return map;
    }

    @Override
    public Map<String, Object> batchDelete(String ids){
        Map<String, Object> map = new HashMap<>();
        try{
            String [] deleteIds = ids.split("-");
            for(String deleteId:deleteIds){
                Integer id = Integer.parseInt(deleteId);
                shoppingCartDao.delete(id);
            }
        }catch (Exception e){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
        }
        return map;
    }

    @Override
    public Map<String, Object> listAll(Integer userId) {
        Map<String, Object> map = new HashMap<>();
        try{
            List<ShoppingCart> shoppingCarts = shoppingCartDao.selectByUserId(userId);
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
            map.put("lists", shoppingCarts);
        }catch (Exception e){
            map.put("code",ResponseCode.SERVER_ERROR.getValue());
        }
        return map;
    }
}
